package com.dongdongshop.shiro;


import com.alibaba.dubbo.config.annotation.Reference;
import com.dongdongshop.pojo.TbSeller;
import com.dongdongshop.service.SellerService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.*;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.apache.shiro.util.ByteSource;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

public class LoginRealm extends AuthorizingRealm {

    @Reference
    private SellerService sellerService;


    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
        //授权方法
        System.out.println("进入授权方法");
        return null;
    }

    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {
        //认证方法
        //1. 取出账号和 用户名
        UsernamePasswordToken token = (UsernamePasswordToken) authenticationToken;
        TbSeller tbSeller = sellerService.selectByName(token.getUsername());

        if (!token.getUsername().equals(tbSeller.getSellerId())){
            return null;
        }
        if (tbSeller.getStatus().equals("1")){
            System.out.println("账户未审核");
            return null;
        }

        return new SimpleAuthenticationInfo(tbSeller,tbSeller.getPassword(), ByteSource.Util.bytes(tbSeller.getBankSeller()),tbSeller.getSellerId());
    }
}
